﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class CategoryDal
    {
        #region CategoryDal private
        private static void SetInfo(IDataRecord reader, ref CategoryEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<CategoryEntity> categoryList)
        {
            while (reader.Read())
            {
                var o = new CategoryEntity();
                SetInfo(reader, ref o);
                categoryList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        public static List<CategoryEntity> GetListByParentId(int typeId, int parentId, int isActive)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstCategory = new List<CategoryEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Category_GetByParentId", typeId, parentId, isActive);
                SetInfoList(ref reader, ref lstCategory);
                return lstCategory;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Category_GetByParentId: {0}", ex.Message));
            }            
        }

        public static CategoryEntity GetById(int id)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var category = new CategoryEntity();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Category_GetById", id);
                if (reader.Read())
                    SetInfo(reader, ref category);
                return category;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Category_GetById: {0}", ex.Message));
            }                  
        }

        public static CategoryEntity GetByUrl(string categoryName)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var category = new CategoryEntity();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Category_GetByUrl", categoryName);
                if (reader.Read())
                    SetInfo(reader, ref category);
                return category;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Category_GetByUrl: {0}", ex.Message));
            }
        }

        public static void Save(CategoryEntity o)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                if (o.CategoryId > 0)
                {
                    SqlHelper.ExecuteNonQuery(
                    DbCommon.ConnectionString,
                    DbCommon.DatabaseSchema + "Category_Update",
                    o.CategoryId, o.TypeId, o.ParentId, o.Priority, o.Name, o.Url, o.ImagePath, o.IsActive); 
                }
                else
                {
                    SqlHelper.ExecuteNonQuery(
                        DbCommon.ConnectionString,
                        DbCommon.DatabaseSchema + "Category_Create",
                        o.TypeId, o.ParentId, o.Priority, o.Name, o.Url, o.ImagePath, o.IsActive);    
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Category Save: {0}", ex.Message));
            }
        }

        public static void SavePriority(int id, int priority)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteNonQuery(
                DbCommon.ConnectionString,
                DbCommon.DatabaseSchema + "Category_Update_Priority", id, priority);                                
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Category Save Priority: {0}", ex.Message));
            }
        }

        public static void Delete(int id)
        {
            try
            {
                if (id > 0)
                    SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Category_Delete", id);                
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Category Delete: {0}", ex.Message));
            }
        }
    }
}
